package com.example.book.service;


import com.example.book.enums.BookStatus;
import com.example.book.mapper.BookMapper;
import com.example.book.model.BookInfo;
import com.example.book.model.PageRequest;
import com.example.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        //1. 获取记录总数
        //2. 获取当前页的信息
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.queryListByLimit(pageRequest);
        for (BookInfo bookInfo: bookInfos){
            bookInfo.setStatusCN(BookStatus.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(count, bookInfos, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(BookStatus.DELETE.getCode());
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookMapper.batchUpdateBook(ids);
    }
}
